<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">用户注册</h3>
      <el-form-item prop="nickName">
        <el-input
          v-model="loginForm.nickName"
          type="text"
          auto-complete="off"
          placeholder="用户名"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="gender">
        <el-select v-model="loginForm.gender" placeholder="性别">
          <el-option label="男" value="1"> </el-option>
          <el-option label="女" value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="address">
        <el-cascader
          v-model="loginForm.address"
          :options="options"
          placeholder="地区"
        ></el-cascader>
      </el-form-item>
      <el-form-item prop="telNumber">
        <el-input
          v-model="loginForm.telNumber"
          type="text"
          auto-complete="off"
          placeholder="手机号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          :type="flag ? 'text' : 'password'"
          auto-complete="off"
          placeholder="密码"
        >
          <i
            slot="suffix"
            :class="flag ? 'el-icon-minus' : 'el-icon-view'"
            style="margin-top: 16px; font-size: 18px"
            autocomplete="auto"
            @click="flag = !flag"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="loginForm.confirmPassword"
          :type="cflag ? 'text' : 'password'"
          auto-complete="off"
          placeholder="确认密码"
        >
          <i
            slot="suffix"
            :class="cflag ? 'el-icon-minus' : 'el-icon-view'"
            style="margin-top: 16px; font-size: 18px"
            autocomplete="auto"
            @click="cflag = !cflag"
          />
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          @click="register"
          size="medium"
          type="primary"
          style="width: 100%; height: 50px"
        >
          <span>注 册</span>
        </el-button>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          @click="$router.push('/login')"
          size="medium"
          type="primary"
          style="width: 100%; height: 50px"
        >
          <span>返 回</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import options from "@/utils/city";
import { register, upFile } from "@/api/login";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        address: [],
      },
      loginRules: {
        nickName: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        gender: [{ required: true, trigger: "change", message: "请选择性别" }],
        address: [{ required: true, trigger: "change", message: "请选择地区" }],
        telNumber: [
          {
            required: true,
            validator: (rule, value, callback) => {
              let reg = /^1[345789]\d{9}$/;
              if (!reg.test(value)) {
                callback(new Error("请确认手机号格式正确"));
              } else {
                callback();
              }
            },
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        confirmPassword: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error("请再次输入您的密码"));
              }
              if (value !== this.loginForm.password) {
                callback(new Error("请确认二次输入密码正确"));
              }
              callback();
            },
            trigger: "blur",
          },
        ],
      },
      options,
      flag: false,
      cflag: false,
    };
  },
  methods: {
    async register() {
      let params = {
        ...this.loginForm,
        address: this.loginForm.address.join("/"),
      };
      let valid = false;
      this.$refs["loginForm"].validate((v) => (valid = v));
      if (!valid) return;
      let res = await register(params);
      this.$modal.msgSuccess(res.data.msg);
      this.$router.push("/login");
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  font-size: 24px;
  margin: 0px auto 30px auto;
  text-align: center;
  color: #fff;
}

.login-form {
  border-radius: 6px;
  background: transparent;
  width: 550px;
  padding: 25px;
  .el-select,
  .el-cascader {
    width: 100%;
  }
  .el-input {
    height: 50px;
    input {
      height: 50px;
    }
  }
  .el-input__inner {
    color: #fff;
    background: rgba(255, 255, 255, 0.3);
    border: none;
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
